using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace AutoUpdate.DAL
{
    public static class SQLHelper
    {
        public static readonly string ConnStr = ConfigurationManager.ConnectionStrings["DBHelper"].ConnectionString;
        public static int ExecuteNonQuery(string sql,SqlParameter[] paras = null,CommandType commtype = CommandType.Text)
        {
            using(SqlConnection conn = new SqlConnection(ConnStr))
            {
                using(SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    if(paras != null)
                    {
                        cmd.Parameters.AddRange(paras);
                    }
                    cmd.CommandType = commtype;
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        public static int ExecuteNonQueryTran(string sql,SqlParameter[] paras = null,CommandType commtype = CommandType.Text)
        {
            SqlConnection conn = new SqlConnection(ConnStr);
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlTransaction tran = null;
            int result = 0;
            try
            {
                if (paras != null)
                {
                    cmd.Parameters.AddRange(paras);
                }
                tran = conn.BeginTransaction();
                cmd.CommandType = commtype;
                cmd.Transaction = tran;
                result = cmd.ExecuteNonQuery();
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
        public static object ExecuteScalar(string sql,SqlParameter[] paras = null,CommandType commtype = CommandType.Text)
        {
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    if (paras != null)
                    {
                        cmd.Parameters.AddRange(paras);
                    }
                    cmd.CommandType = commtype;
                    return cmd.ExecuteScalar();
                }
            }
        }
        public static SqlDataReader ExecuteReader(string sql, SqlParameter[] paras = null, CommandType commtype = CommandType.Text)
        {
            SqlConnection conn = new SqlConnection(ConnStr);
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlDataReader reader = null;
            try
            {
				conn.Open();
                if(paras != null)
                {
                    cmd.Parameters.AddRange(paras);
                }
                cmd.CommandType = commtype;
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            }
            catch (Exception ex)
            {
                throw;
            }
            return reader;
        }
        public static DataSet ExecuteTable(string sql,SqlParameter[] paras = null,CommandType commtype = CommandType.Text)
        {
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    DataSet ds = new DataSet();
                    SqlDataAdapter adpater = new SqlDataAdapter();
                    conn.Open();
                    if (paras != null)
                    {
                        cmd.Parameters.AddRange(paras);
                    }
                    cmd.CommandType = commtype;
                    adpater.SelectCommand = cmd;
                    adpater.Fill(ds);
                    return ds;
                }
            }
        }
    }
}

